select A.FK_SOCIETE, FK_ETABLISSEMENT,
zppcg151, A.FK_COMPTE , A.FK_AXE0 , A.FK_AXE1 , A.FK_AXE2 , A.FK_AXE3 , A.FK_AXE4 ,  A.FK_AXE5 ,  A.FK_AXE6 ,  A.FK_AXE7 , 
 A.FK_AXE8 ,A.FK_AXE9  , A.FK_RUBRIQUE , CASE when SENS = '1' then sum(MONTANT_ANA_MONNAIE1*SENS) else 0 end as SOLDE_DEBIT_MONNAIE1, 
 CASE when SENS = '-1' then sum(MONTANT_ANA_MONNAIE1*(-SENS)) else 0 end as SOLDE_CREDIT_MONNAIE1, :P_DDPE, :P_DFPE 
 FROM finds.FI_GEN_ECRITURE_ANALYTIQUE A, finds.FI_GEN_PLAN_COMPTABLE B 
 WHERE A.FK_SOCIETE = :P_CSTE
  and DATE_COMPTABLE between to_date(:P_DDPE,'yyyymmdd') and to_date(:P_DFPE,'yyyymmdd')
  and (fK_classe_ecriture is null or fk_classe_ecriture<'IFRS')      
and FK_CENTRALISATEUR = UK_NUMERO_COMPTE
and CODE_PARAM_CODE = :CODE_PARAMETRAGE
and zppcg151 is not null and zppcg151 <> 'INV'
group by A.FK_SOCIETE , A.FK_eTABLISSEMENT, zppcg151, A.FK_COMPTE , A.FK_AXE0 , 
 A.FK_AXE1 , A.FK_AXE2 , A.FK_AXE3 , A.FK_AXE4 ,  A.FK_AXE5 ,  A.FK_AXE6 ,  A.FK_AXE7 ,  A.FK_AXE8, A.FK_AXE9 , A.FK_RUBRIQUE ,  :P_DDPE, :P_DFPE , SENS 
union
Select  A.FK_SOCIETE,  FK_ETABLISSEMENT,
 zppcg151, FK_COMPTE , null , null , null , null ,null ,  null , null ,  null , 
 null ,null , null ,
  CASE when FK_SENS = '1' then sum(MONTANT_M1*FK_SENS) else 0 end as SOLDE_DEBIT,
 CASE when FK_SENS = '-1' then sum(MONTANT_M1*(-FK_SENS)) else 0 end as SOLDE_CREDIT, :P_DDPE, :P_DFPE 
from finds.FI_GEN_ECRITURE_GENE A, finds.FI_GEN_PLAN_COMPTABLE
where A.FK_SOCIETE = :P_CSTE 
    and DATE_COMPTABLE between to_date(:P_DDPE,'yyyymmdd')  and to_date(:P_DFPE,'yyyymmdd')
    and (fk_classe_ecriture <> 'IFRS' or fk_classe_ecriture is null)
and FK_CENTRALISATEUR = UK_NUMERO_COMPTE
and CODE_PARAM_CODE = :CODE_PARAMETRAGE
and zppcg151 is not null and zppcg151 <> 'INV'
group by A.FK_SOCIETE , FK_eTABLISSEMENT, zppcg151 , A.FK_COMPTE , :P_DDPE, :P_DFPE , FK_SENS 
